#ifndef _GTDESCR_H_
#define _GTDESCR_H_

#include <sched.h>
#include <ucontext.h>

#include "gtconst.h"

typedef unsigned long int gtthread_t;

typedef struct _gtthread_struct gtthread;

typedef gtthread *p_gtthread;

typedef int handle_t;

typedef struct _list {
    struct _list *next;
    gtthread_t  tid;
} list;

struct _gtthread_struct {
	ucontext_t  context;
    p_gtthread  p_livenext;                 /* The next gtthread in running queue */
    p_gtthread  p_liveprev;                 /* The prev gtthread in running queue */
    p_gtthread  p_waitnext;                 /* The next gtthread in waiting queue */
    p_gtthread  p_waitprev;                 /* The prev gtthread in waiting queue */

    int         active;                     /* Set 0 when the start_routine is finished */
    gtthread_t  tid;                        /* thread id */
    void        (*cleanup)(p_gtthread);       /* Clean up function: free memory, update queues */

    char        *stack;                     /* User defined stack for thread */
    void        *(*start_routine)(void*);   /* Start routine */
    void        *args;                      /* Parameters passed to start routine */
    void        *p_retval;                   /* as the ret_val in gtthread_join(gtthread_t, retval) */
//    void        *p_joined_retval;

    int         policy;                     /* Scheduling policy */
    int         priority;                   /* TODO */
    p_gtthread  p_joining;                  /* Which thread is it joining */
//   int         join_cnt;                   /* increase or decrease if thread joins a thread or woken by the joined thread */
//    list        *p_follower_head;           /* A single linked direction list of threads joining this one */
    int         used;
    int         yield;
};

struct gtthread_mutex {
    long lock;          // Exclusive access to mutex state:
                        // 0: unlocked/free
                        // 1: locked - no other waiters
                        //-1: locked - with possible other waiters
    //long recursion;     // Number of unlocks a thread needs to perform
                        // before the lock is released (recursive mutexes only)
    //int kind;           // Mutex type
    gtthread_t owner;   // Thread owning the mutex
    //handle_t event;     // Mutex release notification to waiting threads
    int initialized;    // For EINVAL
};

typedef struct gtthread_mutex gtthread_mutex_t;


#endif /* _GTDESCR_H_ */
